﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class TablesDAO
    {
        DataServices m_TablesDAO = new DataServices();

        public DataTable GetAll()
        {
            m_TablesDAO.Load("GetAllTables", null);
            return m_TablesDAO;
        }

        public DataTable GetById(String tablesId)
        {
            SqlParameter param = new SqlParameter("@TablesId", SqlDbType.VarChar, 6);
            param.Value = tablesId;

            m_TablesDAO.Load("GetTableById", param);
            return m_TablesDAO;
        }

        public DataTable GetByArea(String areaId)
        {
            SqlParameter param = new SqlParameter("@AreaId", SqlDbType.VarChar, 6);
            param.Value = areaId;

            m_TablesDAO.Load("GetTableByArea", param);
            return m_TablesDAO;
        }

        public DataTable GetByState(int stateId)
        {
            SqlParameter param = new SqlParameter("@StateId", SqlDbType.Int);
            param.Value = stateId;

            m_TablesDAO.Load("GetTableByState", param);
            return m_TablesDAO;
        }

        public DataTable GetByLikeId(String tablesId)
        {
            SqlParameter param = new SqlParameter("@TablesId", SqlDbType.VarChar, 6);
            param.Value = tablesId;

            m_TablesDAO.Load("GetTableByLikeId", param);
            return m_TablesDAO;
        }

        public DataTable GetByName(String tablesName)
        {
            SqlParameter param = new SqlParameter("@TablesName", SqlDbType.NVarChar, 50);
            param.Value = tablesName;

            m_TablesDAO.Load("GetTableByName", param);
            return m_TablesDAO;
        }

        public DataTable GetByOption(String tablesId, String tablesName, String areaId, int stateId)
        {
            SqlParameter[] param = new SqlParameter[4];

            param[0] = new SqlParameter("@TablesId", SqlDbType.VarChar, 6);
            param[0].Value = tablesId;

            param[1] = new SqlParameter("@TablesName", SqlDbType.NVarChar, 50);
            param[1].Value = tablesName;

            param[2] = new SqlParameter("@AreaId", SqlDbType.VarChar, 6);
            param[2].Value = areaId;

            param[3] = new SqlParameter("@StateId", SqlDbType.Int);
            param[3].Value = stateId;

            m_TablesDAO.Load("GetTableOptionFull", param);
            return m_TablesDAO;
        }

        public int Add(TablesInfo info)
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@TablesId", SqlDbType.VarChar, 6);
            param[0].Value = info.TablesId;

            param[1] = new SqlParameter("@TablesName", SqlDbType.NVarChar, 50);
            param[1].Value = info.TablesName;

            param[2] = new SqlParameter("@StateId", SqlDbType.Int);
            param[2].Value = info.StateId;

            param[3] = new SqlParameter("@AreaId", SqlDbType.VarChar, 6);
            param[3].Value = info.AreaId;

            param[4] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[4].Value = info.Remark;

            return m_TablesDAO.ExecuteUpdate("AddTables", param);
        }

        public int Update(TablesInfo info)
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@TablesId", SqlDbType.VarChar, 6);
            param[0].Value = info.TablesId;

            param[1] = new SqlParameter("@TablesName", SqlDbType.NVarChar, 50);
            param[1].Value = info.TablesName;

            param[2] = new SqlParameter("@StateId", SqlDbType.Int);
            param[2].Value = info.StateId;

            param[3] = new SqlParameter("@AreaId", SqlDbType.VarChar, 6);
            param[3].Value = info.AreaId;

            param[4] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[4].Value = info.Remark;

            return m_TablesDAO.ExecuteUpdate("UpdateTables", param);
        }

        public int Delete(String tablesId)
        {
            SqlParameter param = new SqlParameter("@TablesId", SqlDbType.VarChar, 6);
            param.Value = tablesId;

            return m_TablesDAO.ExecuteNoneQuery("DeleteTables", param);
        }

        public DataRow AddNewRow()
        {
            return m_TablesDAO.NewRow();
        }

        public void AddNewTables(DataRow m_Row)
        {
            m_TablesDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_TablesDAO.ExecuteNoneQuery() > 0;
        }
    }
}
